<template>
  <div v-loading="fullscreenLoading">
    <el-form-item :label="component.attrName" :prop="key + '.value'" :id="key">
      <div class="site-wrap">
        <el-input v-model="formData[key].value" />
        <el-button
          @click="selectAddEvent"
          class="site-btn"
          :class="dk == 1 ? 'ppm-btn-green' : 'ppm-btn-orange'"
          >选址</el-button
        >
      </div>
    </el-form-item>
    <el-dialog
      class="ne-pending-task"
      append-to-body
      width="98vw"
      height="96vh"
      top="2vh"
      title="选址"
      v-if="pendItemVisible"
      :visible.sync="pendItemVisible"
      :close-on-click-modal="false"
    >
      <select-address
        @selValueFun="selValueFun"
        :show.sync="pendItemVisible"
        :saParams="saParams"
      ></select-address>
    </el-dialog>
  </div>
</template>

<script>
import mixinstype20 from "../../commons/mixins/mixinstype20";
import { mapState } from "vuex";
import service from "../../utils/service";
import SelectAddress from "../a-to-b/SelectAddress";
import { JSEncrypt } from "jsencrypt";
export default {
  name: "SiteSeletction",
  mixins: [mixinstype20],
  computed: {
    key() {
      return this.component.pageTempStructId + "-" + this.component.attrId;
    },
    dk() {
      return this.component.attrName.includes("甲端") ? 1 : 2;
    },
   prodName(){
     return this.component.actionValue.split('|')[1]
   },
    ...mapState("orders", ["orderPosInfo"]),
    ...mapState("user", ["user"]),
  },
  data() {
    return {
      fullscreenLoading: false,
      secretKey:
        "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCuRmReTzsDwnrGsVkPxnAbeMFpABstpoBtof4fwu0TChN988MLuMEWZDN9cxdSkI9QGH7HPFsvtnClXsjWdJeeBBavBEoiOI7r5re70rc7PdnRbAlVAFAaSHFz5Nc6SYwqqmWvXkhzDxWexMOByI8rBiRNc6gB/JUq3KtdxU3PiQIDAQAB",
      saParams: null,
      pendItemVisible: false,
      ppmCondition: {}, //缓存取得数据
      productId: "",
    };
  },
  components: {
    SelectAddress,
  },
  methods: {
    async selectAddEvent() {
      this.fullscreenLoading = true;
      const result = await service.applyDcoosToken({
        sysUserId: this.user.sysUserId,
        orgId: this.user.orgId,
        prodId: this.ppmCondition.productId,
        scopFlag: 1,
      });
      if (result.meta) {
        if (~~result.meta.resultCode == 1) {
          let userInfo = {
            userName: result.meta.systemUserInfo.staffName,
            contact: result.meta.systemUserInfo.staffName,
            telephone: result.meta.systemUserInfo.mobilePhone,
            email: result.meta.systemUserInfo.eMail,
          };
          this.saParams = {
            token: encodeURIComponent(result.meta.token),
            addressNum: 1,
            productCode: result.meta.prodNbr,
            dk: this.dk,
          };
          this.saParams.userInfo = encodeURIComponent(
            this.getRsaCode(userInfo, this.secretKey)
          );
          this.pendItemVisible = true;
        } else {
          this.$message({
            showClose: true,
            message: result.meta.resultMsg,
            type: "error",
          });
        }
      } else {
        this.$message({
          showClose: true,
          message: result.meta.logMap.stackTrace,
          type: "error",
        });
      }
      this.fullscreenLoading = false;
    },
    async selValueFun(arg) {
      
      if (!(arg.addressesList instanceof Array)) {
        return;
      }
      let addressList = arg.addressesList[0];
      this.$emit("getPluginValue", { value: addressList, key: this.key });
      this.formData[this.key].value = addressList.fullName;
      this.formData[this.prodName + "-142002230"].value = addressList.fullName;
      this.formData[this.prodName + "-142002231"].value = addressList.provinceId;
      this.formData[this.prodName + "-142002232"].value = addressList.cityId;
      this.formData[this.prodName + "-142002233"].value = addressList.longitude;
      this.formData[this.prodName + "-142002234"].value = addressList.latitude;
      this.formData[this.prodName + "-142002308"].value = addressList.id;
      this.formData[this.prodName + "-142002958"].value = addressList.areaId;
      this.formData[this.prodName + "-142002959"].value = addressList.capability;
      this.formData[this.prodName + "-142002960"].value = addressList.capabilityURL;
    },
    getRsaCode(code, key) {
      let encrypt = new JSEncrypt();
      encrypt.setPublicKey(
        "-----BEGIN PUBLIC KEY-----" + key + "-----END PUBLIC KEY-----"
      );
      let data = encrypt.encrypt(JSON.stringify(code));
      return data;
    },
  },
};
</script>
<style lang="scss" scoped>
.site-wrap {
  display: flex;
  align-items: center;
  .site-btn {
    color: #fff;
  }
}
</style>